/**
 * 解析url参数
 * @example ?id=12345&a=b
 * @return Object {id:12345,a:b}
 */
String.prototype.matchAll = function (reg) {
  let result = this.match(reg)
  if (result) {
    let str = this.replace(result[0], '^'.repeat(result[0].length))
    let macth = str.matchAll(reg) || []
    return [result, ...macth]
  }
}

function urlParse() {
  let url = window.location.search || window.location.hash || '?id=2'
  let obj = {}
  let reg = /[?&](?<key>[^?&]+)=(?<value>[^?&]+)/
  let arr = url.matchAll(reg) || []
  arr.forEach(item => {
    let key = decodeURIComponent(item.groups.key)
    let value = decodeURIComponent(item.groups.value)
    obj[key] = value
  })
  return obj
}


export default urlParse 